package com.aaa.controller;


import com.aaa.entity.F_Menu;
import com.aaa.entity.F_TreeVo;
import com.aaa.service.F_MenuService;
import com.aaa.util.EmpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tree")
public class F_LoginController {
    @Autowired
    F_MenuService service;
    /**
     * 菜单树
     * @return
     */
    @RequestMapping("/denglu")
    public  String app(){
        return  "kuangjia/kuangjia";
    }

    /**
     *
     * 菜单树
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object tree(){
        //获取所有的菜单
      //  System.out.println(EmpUtil.getEmp().getUsername()+"11");
        List<F_Menu> list  = service.selectList();
        List<F_TreeVo> treeList = new ArrayList<F_TreeVo>();//记录树菜单数据
        int m = 0;//标记treeList的长度不会随每次添加数据而改变,防止报错
        for (F_Menu t: list) {
            Integer pid = t.getPid();//获取菜单的父id   1    0    系统管理   url
            F_TreeVo ovo = new F_TreeVo();//树数据vo类
            ovo.setId(t.getId());
            ovo.setLabel(t.getName());
            ovo.setUrl(t.getUrl());
            ovo.setPid(t.getPid());
            ovo.setChildren(new ArrayList<F_TreeVo>());
            m = treeList.size();
            if(pid==0){
                treeList.add(ovo);//一级菜单直接加入
            }else{
                for (int i=0;i<m;i++) {//二级菜单,按pid添加到一级菜单下
                    F_TreeVo vo = treeList.get(i);
                    if(vo!=null && pid==vo.getId()){
                        vo.getChildren().add(ovo);
                        break;
                    }
                }
            }

        }

       //System.out.println(treeList+"1111111111111111111111111111111");
        return treeList;
    }

    /**
     * 根据登录人显示不同的权限
     * @return
     */
    @RequestMapping("/listid")
    @ResponseBody
    public Object gettree(){
        //获取所有的菜单
    //   System.out.println(EmpUtil.getEmp().getUsername()+"11");
       int roleid=service.getRoleid();
        String state=service.getstate();
        System.out.println(state+"!");

        List<F_Menu> list;
        if (roleid ==1) {
            list  = service.selectList();
        }
        else {
            System.out.println(roleid);
            list  = service.selectListid(roleid);
        }
        List<F_TreeVo> treeList = new ArrayList<F_TreeVo>();//记录树菜单数据
        int m = 0;//标记treeList的长度不会随每次添加数据而改变,防止报错
        for (F_Menu t: list) {
            Integer pid = t.getPid();//获取菜单的父id   1    0    系统管理   url
            F_TreeVo ovo = new F_TreeVo();//树数据vo类
            ovo.setId(t.getId());
            ovo.setLabel(t.getName());
            ovo.setUrl(t.getUrl());
            ovo.setPid(t.getPid());
            ovo.setChildren(new ArrayList<F_TreeVo>());
            m = treeList.size();
            if(pid==0){
                treeList.add(ovo);//一级菜单直接加入
            }else{
                for (int i=0;i<m;i++) {//二级菜单,按pid添加到一级菜单下
                    F_TreeVo vo = treeList.get(i);
                    if(vo!=null && pid==vo.getId()){
                        vo.getChildren().add(ovo);
                        break;
                    }
                }
            }

        }

      //System.out.println(treeList+"1111111111111111111111111111111");
        return treeList;
    }

    /**
     * 添加一级菜单
     * @param map
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Integer add(@RequestBody Map map){
        int row=service.add(map);
        return row;
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @RequestMapping("/del")
    @ResponseBody
    public Integer del(Integer id){
       // System.out.println(id+"ssssssss");
        int row=service.del(id);
        return row;
    }

    /**
     * 添加二级菜单
     * @param map
     * @return
     */
    @RequestMapping("/addErJi")
    @ResponseBody
    public Integer updateYIji(@RequestBody Map map){
        System.out.println(map);
        int row=service.InsertErji(map);
        return row;
    }

    /**
     * 修改菜单
     * @param map
     * @return
     */
    @RequestMapping("/updateErji")
    @ResponseBody
    public Integer updateErji(@RequestBody Map map){
        System.out.println(map);
        int row=service.updateErji(map);
        return row;
    }
}
